package com.share.device.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.web.page.TableDataInfo;
import com.share.device.domain.Area;
import com.share.device.domain.Station;
import com.share.device.domain.vo.AreaVo;
import com.share.device.service.AreaService;
import com.share.device.service.StationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("area")
public class AreaController extends BaseController {
    @Autowired
    private AreaService areaService;
    @Autowired
    private StationService stationService;

    /**
     * 区域列表
     * @param area
     * @return
     */
    @GetMapping("list")
    public TableDataInfo list(Area area){
        startPage();
        List<Area> list = areaService.list(area);
        return getDataTable(list);
    }

    /**
     * 添加区域
     * @param area
     * @return
     */
    @PostMapping
    public AjaxResult add(@RequestBody @Validated Area area){
        return success(areaService.save(area));
    }

    /**
     * 修改区域
     * @param area
     * @return
     */
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated Area area){
        return success(areaService.updateById(area));
    }
    /**
     * 删除区域
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Integer id){
        return success(areaService.removeById(id));
    }

    /**
     * 获取区域详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Integer id){
        Area area = areaService.getById(id);
        List<Station> list = stationService.getStationListByAreaId(id);
        AreaVo areaVo = new AreaVo();
        areaVo.setAreaName(area.getAreaName());
        areaVo.setStationList(list);
        return success(areaVo);
    }

    /**
     * 获取区域下拉框
     * @return
     */
    @GetMapping("/select")
    public AjaxResult select(){
        List<Area> list = areaService.list();
        return success(list);
    }
}
